package com.innovation.chengzhangriji.ui;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.BabyAndParent;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.BabyAddRequestBody;
import com.innovation.chengzhangriji.http.BabyPhotoUpdateResponse;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.BabyDetailAdapter;
import com.innovation.chengzhangriji.ui.widget.CuteBabyAvatarView;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.util.BabyUtil;
import com.innovation.chengzhangriji.util.FileUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

public class BabyDetailActivity extends BaseActivity implements View.OnClickListener, CuteToolbar.OnClickCuteToolbarListener {
    private RelativeLayout relativeLayout_name, relativeLayout_sex, relativeLayout_ready_day, relativeLayout_yima_time, relativeLayout_bir_day, relativeLayout_note, relativeLayout_xuexing;
    private CuteTextView tv_name, tv_sex, tv_ready_day, tv_bir_day, tv_xuexing, tv_mDay, tv_note, tv_yima_day;
    private ImageView iv_next, iv_erweima;
    private boolean isAdmin = false;
    private Calendar calendar;
    private DatePickerDialog datePickerDialog;
    private String yy, date, dd, mUrl, mName, mDay, emptyUrl, mOldRole,nowdate;
    private long time_ready, time_bir, time_yima;
    private Baby babys;
    private DisplayImageOptions options;
    private CuteBabyAvatarView iv_head;
    private HeadInfo mHeadInfo;
    private RecyclerView recyclerView;
    private BabyDetailAdapter babyDetailAdapter;
    private ArrayList<String> list_touxiang;
    private String gender = "", imageUrl = "";
    private int bb_gender = -1, state = -1;
    private CuteToolbar toolbar;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Config.ChangeBabyname:
                    tv_name.setText(data.getStringExtra("name"));
                    tv_note.setText(data.getStringExtra("name")+"的成长日记");
                    toolbar.setCuteTitle(data.getStringExtra("name"));
                    break;
                case Config.ChangeBabynoteName:
//                    tv_note.setText(data.getStringExtra("name"));
                    break;
                case Config.ChangeBabySex:
                    tv_sex.setText(data.getStringExtra("sex"));
                    if (data.getStringExtra("sex").toString().trim().contains("男")) {
//                        iv_sex.setVisibility(View.VISIBLE);
//                        iv_sex.setImageResource(R.mipmap.icon_nan);
                        iv_head.setSex(1);
                    } else if (data.getStringExtra("sex").toString().trim().contains("女")) {
//                        iv_sex.setVisibility(View.VISIBLE);
//                        iv_sex.setImageResource(R.mipmap.icon_nv);
                        iv_head.setSex(2);
                    } else {
//                        iv_sex.setVisibility(View.INVISIBLE);
                        iv_head.setSex(3);
                    }
                    break;
                case Config.ChangeBabyXuexing:
                    tv_xuexing.setText(data.getStringExtra("xuexing"));
                    break;
                case Config.REQUEST_OPEN_CAMERA:
                    imageUrl = data.getStringExtra(Config.ACTION_EXTERNAL_RESULT_PHOTO);
                    break;
                case Config.REQUEST_OPEN_PHOTO:
                    imageUrl = data.getStringExtra(Config.ACTION_EXTERNAL_RESULT_PHOTO);
                    break;

            }
            if (!TextUtils.isEmpty(imageUrl)) {
                File imgFile = new File(imageUrl);
                if (imgFile.exists()) {
                    emptyUrl = imageUrl;
                    mHeadInfo.setHasHead(true);
                    mHeadInfo.setHeadExtension(FileUtils.getFileExtension(imageUrl));
                    mHeadInfo.setHeadBase64(FileUtils.encodeFileToBase64Binary(imgFile));
                    // ImageLoader.getInstance().displayImage("file://" + imageUrl, iv_head);
                    iv_head.setAvatar("file://" + imageUrl);
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_detail);
        toolbar = (CuteToolbar) findViewById(R.id.baby_detail_toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        mHeadInfo = new HeadInfo();
        setOptions();

        mLoginInfo = Config.getLoginInfo(BabyDetailActivity.this);
        String userId = mLoginInfo.getUserID();
        String phone = mLoginInfo.getPhone();
        String token = mLoginInfo.getToken().getAccessToken();
        final int babyId = mLoginInfo.getBabyID();
        BabyAndParent babyAndParent = DatabaseManager.getInstance(BabyDetailActivity.this).queryBabyAndParent(userId, babyId);
        babys = DatabaseManager.getInstance(BabyDetailActivity.this).queryBaby(userId, babyId);
        mOldRole = babys.getParentName();
        toolbar.setCuteTitle(babys.getName());
        mUrl = babys.getImageUrl();
        mName = babys.getName();
        mDay = BabyUtil.getBabyDay(babys, System.currentTimeMillis());
        Log.e("mday", mDay);

        isAdmin = babyAndParent.getRole() == 2;
        calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        date = df.format(new Date());
        nowdate = df.format(new Date());
        init();
    }

    private void init() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_baby_detail);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        babyDetailAdapter = new BabyDetailAdapter();
        babyDetailAdapter.setSex(babys.getGender());
        recyclerView.setAdapter(babyDetailAdapter);
        list_touxiang = new ArrayList<String>();
        babyDetailAdapter.setList(list_touxiang);

        setupData();

        // iv_sex = (ImageView) findViewById(R.id.iv_byby_detail_for_sex);
        iv_head = (CuteBabyAvatarView) findViewById(R.id.iv_baby_detail);
        iv_head.setOnClickListener(this);
        // ImageLoader.getInstance().displayImage(babys.getImageUrl(), iv_head, options);
        iv_head.setAvatar(babys.getImageUrl());
        iv_head.setSex(babys.getGender());
        tv_mDay = (CuteTextView) findViewById(R.id.baby_detail_time);
        tv_mDay.setText(mDay);

        relativeLayout_yima_time = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_yima_time);
        relativeLayout_yima_time.setOnClickListener(this);
        relativeLayout_note = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_note);
//        relativeLayout_note.setOnClickListener(this);
        relativeLayout_name = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_name);
        relativeLayout_name.setOnClickListener(this);
        relativeLayout_sex = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_sex);
        relativeLayout_sex.setOnClickListener(this);
        relativeLayout_ready_day = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_ready_day);
        relativeLayout_ready_day.setOnClickListener(this);
        relativeLayout_bir_day = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_bir_day);
        relativeLayout_bir_day.setOnClickListener(this);
//        relativeLayout_xuexing = (RelativeLayout) findViewById(R.id.relativeLayout_babydetail_xuexing);
//        relativeLayout_xuexing.setOnClickListener(this);

        tv_name = (CuteTextView) findViewById(R.id.baby_detail_name);
        tv_name.setText(babys.getName());
        tv_note = (CuteTextView) findViewById(R.id.baby_detail_note);
//        tv_note.setText(babys.getNoteName());
        tv_note.setText(babys.getName()+"的成长日记");

        tv_sex = (CuteTextView) findViewById(R.id.baby_detail_sex);
        tv_sex.setText(getGender(babys.getGender()));
//        switch (babys.getGender()) {
//            case 1:
//                iv_sex.setVisibility(View.VISIBLE);
//                iv_sex.setImageResource(R.mipmap.icon_nan);
//                break;
//            case 2:
//                iv_sex.setVisibility(View.VISIBLE);
//                iv_sex.setImageResource(R.mipmap.icon_nv);
//                break;
//            case 0:
//                iv_sex.setVisibility(View.INVISIBLE);
//                break;
//            default:
//                iv_sex.setVisibility(View.INVISIBLE);
//                break;
//        }
        tv_yima_day = (CuteTextView) findViewById(R.id.baby_detail_yima_day);
        tv_yima_day.setText(getDate(babys.getLastDate()));
        if (babys.getLastDate() != Client.EMPTY_TIME) {
            time_yima = babys.getLastDate();
        }
        tv_ready_day = (CuteTextView) findViewById(R.id.baby_detail_ready_day);
        tv_ready_day.setText(getDate(babys.getDueDate()));
        if (babys.getDueDate() != Client.EMPTY_TIME) {
            time_ready = babys.getDueDate();
        }
        tv_bir_day = (CuteTextView) findViewById(R.id.baby_detail_bir_day);
        tv_bir_day.setText(getDate(babys.getBirthday()));
        if (babys.getBirthday() != Client.EMPTY_TIME) {
            time_bir = babys.getBirthday();
        }
//        tv_xuexing = (CuteTextView) findViewById(R.id.baby_detail_xuexing);
//        tv_xuexing.setText(babys.getName());
        iv_erweima = (ImageView) findViewById(R.id.iv_detail_small_erweima);
        iv_erweima.setOnClickListener(this);
    }

    // 加载数据
    private void setupData() {
        // 加载历史头像
        Client.requestHistoryAvatar(this.getApplicationContext(), mLoginInfo.getPhone(), mLoginInfo.getToken().getAccessToken(), babys.getBabyID().toString(), 0, new ResponseHandler() {
            @Override
            public void onInnovationSuccess(JsonElement value) {
                super.onInnovationSuccess(value);
                list_touxiang.clear();
                String[] avatarArray = get(value.toString(), String[].class);
                Collections.addAll(list_touxiang, avatarArray);
                if(list_touxiang.size() > 0) {
                    list_touxiang.remove(0); // 移除当前头像
                }
                babyDetailAdapter.setList(list_touxiang);
                babyDetailAdapter.notifyDataSetChanged();
            }
        }, new Client.NetworkUnavailableListener() {
            @Override
            public void networkUnavailable() {
                tipShow(getString(R.string.tip_network_unavailable));
            }
        });
    }

    @Override
    public void onClickLeft() {
        if (tv_name.getText().equals(babys.getName()) &&
                time_bir ==0 && time_yima==0 && time_ready ==0){
            onBackPressed();
        }else {
            if (TextUtils.isEmpty(tv_name.getText()) || TextUtils.isEmpty(tv_name.getText())) {
                tipShow("请填写完整的信息");
                return;
            } else {
                result();
            }
        }
//        onBackPressed();
    }

    private void openDialog() {
        final CharSequence[] items = {"拍照", "从手机相册选择"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("添加头像");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                switch (item) {
                    case 0:
                        startHeadCameraForResult();
                        break;
                    case 1:
                        startHeadSelectForResult();
                        break;
                }
            }
        });
        builder.create().show();
    }

    private void startHeadCameraForResult() {
        Intent intent = new Intent(BabyDetailActivity.this, HeadCameraActivity.class);
        startActivityForResult(intent, Config.REQUEST_OPEN_CAMERA);
    }

    private void startHeadSelectForResult() {
        Intent intent = new Intent(BabyDetailActivity.this, HeadPhotoActivity.class);
        startActivityForResult(intent, Config.REQUEST_OPEN_PHOTO);
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.gray_head)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.mipmap.gray_head)  //image连接地址为空时
                .showImageOnFail(R.mipmap.gray_head)  //image加载失败
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }

    private String getGender(int state) {
        switch (state) {
            case Client.BOY:
                gender = "男";
                break;
            case Client.GIRL:
                gender = "女";
                break;
            case Client.UNKNOWN:
                gender = "未知";
                break;
        }
        return gender;
    }

    @Override
    public void onClickRight() {
        // 判断是否有数据为空
//        if (TextUtils.isEmpty(tv_name.getText()) || TextUtils.isEmpty(tv_name.getText())) {
//            tipShow("请填写完整的信息");
//            return;
//        } else {
//            result();
//        }
//        if (checkChange()) {
//            Log.e("asd","asd2");
//            result();
//        } else {
//            Log.e("asd","asd3");
//            finish();
//        }
    }

    private void openQRDialog() {
        Intent intent = new Intent(this, BabyQRCodeDialogActivity.class);
        intent.putExtra("url", mUrl);
        intent.putExtra("name", mName);
        intent.putExtra("day", mDay);
        startActivity(intent);
    }

    //    public Baby getBaby() {
//        return baby;
//    }
//    private boolean checkChange() {
//        // 判断图片路径是否为空
//        if (!TextUtils.isEmpty(emptyUrl)) {
//            return true;
//        }
//        // 判断当前宝贝信息是否和原信息相同
//        LoginInfo loginInfo = Config.getLoginInfo(this);
//        if (loginInfo != null) {
//            String userId = loginInfo.getUserID();
//            int babyID = loginInfo.getBabyID();
//
//            return !(baby.getName().equals(getBaby().getName()) &&
//                    baby.getGender().equals(getBaby().getGender()) &&
//                    baby.getParentName().equals(getBaby().getParentName()) &&
//                    baby.getLastDate().equals(getBaby().getLastDate()) &&
//                    baby.getDueDate().equals(getBaby().getDueDate()) &&
//                    baby.getBirthday().equals(getBaby().getBirthday()) &&
//                    baby.getImageUrl().equals(getBaby().getImageUrl()) &&
//                    baby.getState().equals(getBaby().getState()));
//        }
//        return false;
//    }
    private void result() {
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        if (!TextUtils.isEmpty(emptyUrl)) {

            babyHeadUpload();
        } else {
            Log.e("asd", "asd4");
            babyUpload();
        }
    }

    private void babyHeadUpload() {
        LoginInfo login = Config.getLoginInfo(this);
        if (login == null) {
            return;
        }
        String phone = login.getPhone();
        String token = login.getToken().getAccessToken();
        String ext = ".jpg";
        String base64 = FileUtils.encodeFileToBase64Binary(new File(emptyUrl));
        Client.requestPictureResourceUpload(this, phone, token, ext, base64, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                Log.d("头像上传成功", "" + value.toString());
//                BabyPhotoUpdateResponse response = get(value.toString(), BabyPhotoUpdateResponse.class);
//                if (response != null) {
//                    if (response.getResult() == 1) {
//                        babys.setImageUrl(response.getImageUrl());
//                        Log.d("网络地址", "" + response.getImageUrl());
//                        babyUpload();
//                    }
//                }
                babys.setImageUrl(get(value.toString(), String.class));
                babyUpload();
                dismissProgressDialog();
            }

            @Override
            public void onChZhRJFailure(String value) {
                tipShow(value);
                dismissProgressDialog();
            }

            @Override
            public void onChZhRJTokenFailure(String value) {
                tipShow(value);
                dismissProgressDialog();
                finish();
            }
        }, networkUnavailableSampleImpl());
    }

    public void updateBaby(Baby baby) {
        DatabaseManager.getInstance(this).updateBaby(baby);
    }

    public void updateTimeline(int babyID, String newRole) {
        if (mOldRole.equals(newRole)) {
            return;
        }
        DatabaseManager.getInstance(this).updateRole(babyID, mOldRole, newRole);
    }

    private void babyUpload() {
        final LoginInfo login = Config.getLoginInfo(BabyDetailActivity.this);
        Log.e("asd", "" + login);
        if (login == null) {
            return;
        }
        final String phone = login.getPhone();
        final String token = login.getToken().getAccessToken();
        final BabyAddRequestBody body = new BabyAddRequestBody();
        body.setName(tv_name.getText().toString());

        switch (tv_sex.getText().toString()) {
            case "男":
                bb_gender = 1;
                break;
            case "女":
                bb_gender = 2;
                break;
            default:
                bb_gender = -1;
                break;
        }
        body.setGender(bb_gender);
        body.setParentName(babys.getParentName());
        body.setLastDate(babys.getLastDate());

        if (time_bir != 0) {
            body.setBirthday(time_bir);
        } else {
            body.setBirthday(babys.getBirthday());
        }
        if (time_ready != 0) {
            body.setDueDate(time_ready);
        } else {
            body.setDueDate(babys.getDueDate());
        }
        if (time_yima != 0) {
            body.setLastDate(time_yima);
        } else {
            body.setLastDate(babys.getLastDate());
        }
        if (!imageUrl.equals("")) {
            body.setImageUrl(babys.getImageUrl());
        }
        Log.e("结果", "" + time_bir);
        if (time_bir != 0) {
            body.setState(Client.STATE_END);
            state = Client.STATE_END;
        } else {
            if (time_ready != 0) {
                body.setState(Client.STATE_START);
                state = Client.STATE_START;
            } else {
                body.setState(Client.STATE_READY);
                state = Client.STATE_READY;
            }
        }
//        body.setState(babys.getState());
        Client.requestBabyUpdate(this, phone, token, babys.getBabyID(), babys.getUpdateTime(), body.getBabyAddRequestBody(), new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                babys.setName(tv_name.getText().toString());
                babys.setNoteName(tv_note.getText().toString());
                babys.setGender(bb_gender);
                babys.setParentName(body.getParentName());
                if (time_ready != 0) {
                    babys.setDueDate(time_ready);
                } else {
                    babys.setDueDate(Client.EMPTY_TIME);
                }
                if (time_yima != 0) {
                    babys.setLastDate(time_yima);
                } else {
                    babys.setLastDate(Client.EMPTY_TIME);
                }
                if (time_bir != 0) {
                    babys.setBirthday(time_bir);
                } else {
                    babys.setBirthday(Client.EMPTY_TIME);
                }

                babys.setState(state);
                updateBaby(babys);
                updateTimeline(babys.getBabyID(), babys.getParentName());
                dismissProgressDialog();
//                setResult(Activity.RESULT_OK);
                finish();
            }

            @Override
            public void onChZhRJFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
            }

            @Override
            public void onChZhRJTokenFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
                finish();
            }
        }, networkUnavailableSampleImpl());
    }

    private String getDate(long time) {
        if (time == Client.EMPTY_TIME) {
            return null;
        }
        DateTime dateTime = new DateTime(time);
        return "" + dateTime.getYear() + "-" + dateTime.getMonthOfYear() + "-" + dateTime.getDayOfMonth();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_detail_small_erweima:
                openQRDialog();
                return;
        }
        if (isAdmin) {
            Intent intent = new Intent(BabyDetailActivity.this, PetnameListActivity.class);
            switch (v.getId()) {
                case R.id.iv_baby_detail:
                    openDialog();
                    break;
//                case R.id.iv_detail_small_erweima:
//                    openQRDialog();
//                    break;
                case R.id.relativeLayout_babydetail_note:
//                    Intent intent_note = new Intent(BabyDetailActivity.this, BabyDetailChangeActivity.class);
//                    intent_note.putExtra("name", tv_name.getText().toString().trim());
//                    startActivityForResult(intent_note, Config.ChangeBabynoteName);
                    break;
                case R.id.relativeLayout_babydetail_name:
                    Intent intent_name = new Intent(BabyDetailActivity.this, BabyDetailChangeActivity.class);
                    intent_name.putExtra("name", tv_name.getText().toString().trim());
                    startActivityForResult(intent_name, Config.ChangeBabyname);
                    break;
                case R.id.relativeLayout_babydetail_sex:
                    intent.putExtra("type", "sex");
                    intent.putExtra("sex", tv_sex.getText().toString().trim());
                    intent.putExtra("state", babys.getState()-1);
                    startActivityForResult(intent, Config.ChangeBabySex);
                    break;
//                case R.id.relativeLayout_babydetail_xuexing:
//                    intent.putExtra("type","xuexing");
//                    intent.putExtra("xuexing",tv_xuexing.getText().toString().trim());
//                    startActivityForResult(intent, Config.ChangeBabyXuexing);
//                    break;
                case R.id.relativeLayout_babydetail_yima_time:
//                    calendar.setTime(babys.getLastDate());
                    if (babys.getLastDate() != Client.EMPTY_TIME){
                        date = DateFormat.format("yyyy-MM-dd", babys.getLastDate()).toString();
                    }else {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        date = df.format(new Date());
                    }
                    datePickerDialog = new DatePickerDialog(BabyDetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
                            if (i2 < 9) {
                                yy = "0" + (i2 + 1);
                            } else {
                                yy = "" + (i2 + 1);
                            }
                            if (i3 < 10) {
                                dd = "0" + i3;
                            } else {
                                dd = "" + i3;
                            }
                            date = i + "-" + yy + "-" + dd;
                            tv_yima_day.setText(i + "-" + yy + "-" + dd);
                            calendar.set(i, i2, i3, 0, 0, 0);
                            time_yima = calendar.getTimeInMillis();
                            if (tv_bir_day.getText().toString().equals("") && tv_ready_day.getText().toString().equals("")) {
                                babys.setBirthday(time_yima);
                                babys.setState(Client.STATE_READY);
                                mDay = BabyUtil.getBabyDay(babys, System.currentTimeMillis());
                                tv_mDay.setText(mDay);
                                Log.e("tv_mDay.setText(mDay)1;", mDay);
                            }
                        }
                    }, Integer.parseInt(date.substring(0, 4)), (Integer.parseInt(date.substring(5, 7)) - 1), Integer.parseInt(date.substring(8, 10)));
                    datePickerDialog.show();
                    break;
                case R.id.relativeLayout_babydetail_ready_day:
                    if (babys.getDueDate() != Client.EMPTY_TIME){
                        date = DateFormat.format("yyyy-MM-dd", babys.getDueDate()).toString();
                    }else{
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        date = df.format(new Date());
                    }
                    datePickerDialog = new DatePickerDialog(BabyDetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
                            if (i2 < 9) {
                                yy = "0" + (i2 + 1);
                            } else {
                                yy = "" + (i2 + 1);
                            }
                            if (i3 < 10) {
                                dd = "0" + i3;
                            } else {
                                dd = "" + i3;
                            }
                            date = i + "-" + yy + "-" + dd;
                            tv_ready_day.setText(i + "-" + yy + "-" + dd);
                            calendar.set(i, i2, i3, 0, 0, 0);
                            time_ready = calendar.getTimeInMillis();
                            if (tv_bir_day.getText().toString().equals("")) {
                                babys.setDueDate(time_ready);
                                babys.setState(Client.STATE_START);
                                mDay = BabyUtil.getBabyDay(babys, System.currentTimeMillis());
                                tv_mDay.setText(mDay);
                                Log.e("tv_mDay.setText(mDay)2;", mDay);
                            }
//                            switch (babys.getState()) {
//                                case Client.STATE_READY:
//                                    mDay = getBabyDay(babys);
//                                    break;
//                                case Client.STATE_START:
//                                    mDay = getBabyDay(babys);
//                                    break;
//                                case Client.STATE_END:
//                                    mDay = getBabyDay(babys);
//                                    break;
//                            }

                        }
                    }, Integer.parseInt(date.substring(0, 4)), (Integer.parseInt(date.substring(5, 7)) - 1), Integer.parseInt(date.substring(8, 10)));
                    datePickerDialog.show();
                    break;
                case R.id.relativeLayout_babydetail_bir_day:
                    if (babys.getBirthday() != Client.EMPTY_TIME){
                        date = DateFormat.format("yyyy-MM-dd", babys.getBirthday()).toString();
                    }else{
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        date = df.format(new Date());
                    }
                    datePickerDialog = new DatePickerDialog(BabyDetailActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
                            if (i2 < 9) {
                                yy = "0" + (i2 + 1);
                            } else {
                                yy = "" + (i2 + 1);
                            }
                            if (i3 < 10) {
                                dd = "0" + i3;
                            } else {
                                dd = "" + i3;
                            }
                            date = i + "-" + yy + "-" + dd;
                            tv_bir_day.setText(i + "-" + yy + "-" + dd);
                            calendar.set(i, i2, i3, 0, 0, 0);
                            time_bir = calendar.getTimeInMillis();
                            babys.setBirthday(time_bir);
                            babys.setState(Client.STATE_END);
                            mDay = BabyUtil.getBabyDay(babys, System.currentTimeMillis());
                            tv_mDay.setText(mDay);
                            Log.e("tv_mDay.setText(mDay)3;", mDay);
                        }
                    }, Integer.parseInt(date.substring(0, 4)), (Integer.parseInt(date.substring(5, 7)) - 1), Integer.parseInt(date.substring(8, 10)));
                    datePickerDialog.show();
                    break;

            }
        }
    }

    private class HeadInfo {

        private boolean hasHead;

        private String headBase64;

        private String headExtension;

        private String headServerUrl;

        public boolean isHasHead() {
            return hasHead;
        }

        public void setHasHead(boolean hasHead) {
            this.hasHead = hasHead;
        }

        public String getHeadBase64() {
            return headBase64;
        }

        public void setHeadBase64(String headBase64) {
            this.headBase64 = headBase64;
        }

        public String getHeadExtension() {
            return headExtension;
        }

        public void setHeadExtension(String headExtension) {
            this.headExtension = headExtension;
        }

        public String getHeadServerUrl() {
            return headServerUrl;
        }

        public void setHeadServerUrl(String headServerUrl) {
            this.headServerUrl = headServerUrl;
        }
    }
}
